{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "97b65177-71a5-4c17-b180-a2434c66e3e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>地域</th>\n",
       "      <th>省份</th>\n",
       "      <th>产品年</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>2020</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>2021</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>2022</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B</td>\n",
       "      <td>乙</td>\n",
       "      <td>2020</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>B</td>\n",
       "      <td>乙</td>\n",
       "      <td>2021</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>乙</td>\n",
       "      <td>2022</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>C</td>\n",
       "      <td>丙</td>\n",
       "      <td>2020</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>C</td>\n",
       "      <td>丙</td>\n",
       "      <td>2021</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>C</td>\n",
       "      <td>丙</td>\n",
       "      <td>2022</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   地域 省份   产品年  数量  金额  市值\n",
       "0  A   甲  2020   1  10  15\n",
       "1  A   甲  2021   2  20  30\n",
       "2  A   甲  2022   3  30  45\n",
       "3   B  乙  2020   1  10  15\n",
       "4   B  乙  2021   2  20  30\n",
       "5   B  乙  2022   3  30  45\n",
       "6   C  丙  2020   1  10  15\n",
       "7   C  丙  2021   2  20  30\n",
       "8   C  丙  2022   3  30  45"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pandas.api.types import CategoricalDtype\n",
    "\n",
    "df = pd.read_excel('./test.xlsx',sheet_name='Sheet1')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f1846f1d-4df7-458e-9b4f-a696b43829ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>产品年</th>\n",
       "      <th>2020</th>\n",
       "      <th>2021</th>\n",
       "      <th>2022</th>\n",
       "      <th>All</th>\n",
       "      <th>2020</th>\n",
       "      <th>2021</th>\n",
       "      <th>2022</th>\n",
       "      <th>All</th>\n",
       "      <th>2020</th>\n",
       "      <th>2021</th>\n",
       "      <th>2022</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>市值</th>\n",
       "      <th>市值</th>\n",
       "      <th>市值</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>数量</th>\n",
       "      <th>数量</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>金额</th>\n",
       "      <th>金额</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地域</th>\n",
       "      <th>省份</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>甲</th>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th>乙</th>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <th>丙</th>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <th></th>\n",
       "      <td>45</td>\n",
       "      <td>90</td>\n",
       "      <td>135</td>\n",
       "      <td>270</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>18</td>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "      <td>90</td>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "产品年    2020 2021 2022  All 2020 2021 2022 All 2020 2021 2022  All\n",
       "         市值   市值   市值   市值   数量   数量   数量  数量   金额   金额   金额   金额\n",
       "地域  省份                                                           \n",
       "A   甲    15   30   45   90    1    2    3   6   10   20   30   60\n",
       "B   乙    15   30   45   90    1    2    3   6   10   20   30   60\n",
       "C   丙    15   30   45   90    1    2    3   6   10   20   30   60\n",
       "All      45   90  135  270    3    6    9  18   30   60   90  180"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivot = pd.pivot_table(df,index=['地域','省份'],columns=['产品年'],values=['数量','金额','市值'],aggfunc='sum',margins=True).transpose().swaplevel(0).transpose()\n",
    "df_pivot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d23dbfc1-ae07-472d-a8e0-42be38743ff4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>产品年</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2020</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2021</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2022</th>\n",
       "      <th colspan=\"3\" halign=\"left\">All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地域</th>\n",
       "      <th>省份</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>甲</th>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>90</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th>乙</th>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>90</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <th>丙</th>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>90</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <th></th>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>90</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>135</td>\n",
       "      <td>9</td>\n",
       "      <td>90</td>\n",
       "      <td>270</td>\n",
       "      <td>18</td>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "产品年    2020        2021        2022         All         \n",
       "         市值 数量  金额   市值 数量  金额   市值 数量  金额   市值  数量   金额\n",
       "地域  省份                                                  \n",
       "A   甲    15  1  10   30  2  20   45  3  30   90   6   60\n",
       "B   乙    15  1  10   30  2  20   45  3  30   90   6   60\n",
       "C   丙    15  1  10   30  2  20   45  3  30   90   6   60\n",
       "All      45  3  30   90  6  60  135  9  90  270  18  180"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivot = df_pivot.sort_index(axis=1,level=0)\n",
    "df_pivot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "19c44ef5-3c2d-4445-b5f9-95396903d332",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(2020, '市值'),\n",
       " (2020, '数量'),\n",
       " (2020, '金额'),\n",
       " (2021, '市值'),\n",
       " (2021, '数量'),\n",
       " (2021, '金额'),\n",
       " (2022, '市值'),\n",
       " (2022, '数量'),\n",
       " (2022, '金额'),\n",
       " ('All', '市值'),\n",
       " ('All', '数量'),\n",
       " ('All', '金额')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols = list(df_pivot.columns)\n",
    "cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f46efa01-d141-430c-a329-3c78370ef3e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 0, 4, 5, 3, 7, 8, 6]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols= []\n",
    "for i in range(1,10):\n",
    "    if i % 3 != 0:\n",
    "        cols.append(i)\n",
    "    else:\n",
    "        cols.append(i-3)\n",
    "cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7fcb4d97-2203-40d8-997d-8e10d4457697",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>产品年</th>\n",
       "      <th>地域</th>\n",
       "      <th>省份</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2020</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2021</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2022</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>乙</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>丙</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>All</td>\n",
       "      <td></td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>90</td>\n",
       "      <td>9</td>\n",
       "      <td>90</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "产品年   地域 省份 2020         2021         2022         \n",
       "              数量  金额  市值   数量  金额  市值   数量  金额   市值\n",
       "0     A   甲    1  10  15    2  20  30    3  30   45\n",
       "1      B  乙    1  10  15    2  20  30    3  30   45\n",
       "2      C  丙    1  10  15    2  20  30    3  30   45\n",
       "3    All       3  30  45    6  60  90    9  90  135"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_result = df_pivot.iloc[:,cols].reset_index()\n",
    "df_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2e2ca558-f131-4f78-a2e3-196e962cc22e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(2020, '数量'),\n",
       " (2020, '金额'),\n",
       " (2020, '市值'),\n",
       " (2021, '数量'),\n",
       " (2021, '金额'),\n",
       " (2021, '市值'),\n",
       " (2022, '数量'),\n",
       " (2022, '金额'),\n",
       " (2022, '市值')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构建元组\n",
    "\n",
    "cols_new = []\n",
    "\n",
    "a = [2020,2021,2022]\n",
    "b = ['数量','金额','市值']\n",
    "\n",
    "for i in a:\n",
    "    for m in b:\n",
    "        list_temp = []\n",
    "        list_temp.append(i)\n",
    "        list_temp.append(m)\n",
    "        cols_new.append(tuple(list_temp))\n",
    "        \n",
    "cols_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "edca387c-2bf8-455e-ad09-f2972edcde08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>产品年</th>\n",
       "      <th>地域</th>\n",
       "      <th>省份</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2020</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2021</th>\n",
       "      <th colspan=\"3\" halign=\"left\">2022</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "      <th>市值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>甲</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>乙</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>丙</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>All</td>\n",
       "      <td></td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>45</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>90</td>\n",
       "      <td>9</td>\n",
       "      <td>90</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "产品年   地域 省份 2020         2021         2022         \n",
       "              数量  金额  市值   数量  金额  市值   数量  金额   市值\n",
       "0     A   甲    1  10  15    2  20  30    3  30   45\n",
       "1      B  乙    1  10  15    2  20  30    3  30   45\n",
       "2      C  丙    1  10  15    2  20  30    3  30   45\n",
       "3    All       3  30  45    6  60  90    9  90  135"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_result2 = df_pivot[cols_new].reset_index()\n",
    "df_result2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
